package org.dtrd.modules.record.controller;


import com.alibaba.fastjson.JSONObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.record.entity.bean.SigninBodyDetail;
import org.dtrd.modules.record.entity.bean.SigninBodyListInfo;
import org.dtrd.modules.record.entity.bean.SigninBodyListRequest;
import org.dtrd.modules.record.entity.bean.SigninBodyPageRequest;
import org.dtrd.modules.record.service.IDtrdEntRdSigninBodyService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 人体成分分析表 前端控制器
 * </p>
 * @author sun
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/record/signin/body")
public class DtrdEntRdSigninBodyController extends BaseController {

    @Autowired
    private IDtrdEntRdSigninBodyService signinBodyService;

    @ApiOperation(value = "分页查询人体成分分析表列表", response = SigninBodyListInfo.class)
    @PostMapping("/page")
    @AutoLog(value="查询人体成分分析表列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getSigninBodyList(@RequestBody @Validated SigninBodyPageRequest request){
        handlePageRequest(request);
        IPage<SigninBodyListInfo> page = signinBodyService.pageSigninBody(request);
        JSONObject result = new JSONObject();
        result.put("recordList", page.getRecords());
        result.put("totalPage", page.getPages());
        result.put("count", page.getTotal());
        return Result.OK(result);
    }

    @ApiOperation("新增或修改人体成分分析表")
    @PostMapping("/save")
    @AutoLog(value="新增或修改人体成分分析表", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateSigninBody(@RequestBody SigninBodyDetail dto){
        boolean result = signinBodyService.saveOrUpdateSigninBody(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除人体成分分析表")
    @DeleteMapping("/delete")
    @AutoLog(value="删除人体成分分析表", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeSigninBody(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = signinBodyService.removeSigninBody(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
